<template>
  <!-- hidden PageHeaderWrapper title demo -->
  <page-header-wrapper :title="false">
    <a-card :body-style="{ padding: '24px 32px' }" :bordered="false">
      <a-form @submit="handleAdd" :form="this.form">
        <a-form-item
          label="商品名"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-select v-decorator="['product']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.prodOption" :key="i.value">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="成本"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-input v-decorator="['commCost']" addonAfter="元" placeholder="请输入成本" name="commCost" />
        </a-form-item>
        <a-form-item
          label="串码模式"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-radio-group v-decorator="['chuanma', { initialValue: '1' }]" placeholder="请选择">
            <a-radio :style="radioStyle" :value="1">单串码模式</a-radio>
            <a-radio :style="radioStyle" :value="2">双串码模式</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="串码"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-textarea
            rows="8"
            placeholder="可添加多个,每行一个串码，错误的串码会被过滤掉"
            v-decorator="['phoneCodeList', { rules: [{ required: true, message: '请输入' }] }]"
          />
        </a-form-item>
        <a-form-item
          label="往来账"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-radio-group v-decorator="['isRecord', { initialValue: 1 , rules: [{ required: true, message: '请选择' }]}]" placeholder="请选择">
            <a-radio :style="radioStyle" :value="1">记录供应商往来账</a-radio>
            <a-radio :style="radioStyle" :value="2">不记录供应商往来账</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :wrapperCol="{ span: 24 }" style="text-align: center">
          <a-button htmlType="submit" type="primary" icon="check">添加</a-button>
          <!-- <a-button style="margin-left: 8px">{{ $t('form.basic-form.form.save') }}</a-button> -->
        </a-form-item>
      </a-form>
    </a-card>
    <a-card :body-style="{ padding: '24px 32px' }" title="入库商品清单">
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="isRecord" slot-scope="text, record">
          <template>
            <div v-if="record.isRecord == 1">是</div>
            <div v-else>否</div>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#D15B47" @click="handleDel(record)">删除</a-tag>
          </template>
        </span>
      </a-table>
    </a-card>
    <a-card :body-style="{ padding: '24px 32px' }" :bordered="false">
      <a-form-item :wrapperCol="{ span: 24 }" style="text-align: center">
        <a-button type="primary" :loading="formLoading" @click="handleSubmit()">提交入库</a-button>
      </a-form-item>
    </a-card>
  </page-header-wrapper>
</template>
  <script>
import { getGoodsListUsingPOST } from '@/api/api/shangpinguanli.ts'
import { appendPurchaseUsingPOST } from '@/api/api/caigouguanli.ts'
export default {
  name: 'InStoreRecordAdd',
  data() {
    return {
      radioStyle: {
        display: 'block',
        height: '30px',
        lineHeight: '30px',
      },
      formLoading: false,
      params: {
        buyType: 1,
        channelId: '',
        detailParamList: [],
        memo: '',
        orderDate: '',
        storeId: '',
        supplyId: '',
      },
      queryParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      prodOption: [],
      form: this.$form.createForm(this),
      form1: this.$form.createForm(this),
      columns: [
        {
          title: '商品名',
          dataIndex: 'goodName',
        },
        {
          title: '串码',
          dataIndex: 'phoneCode1',
        },
        {
          title: '串码2',
          dataIndex: 'phoneCode2',
        },
        {
          title: '成本(元)',
          dataIndex: 'commCost',
        },
        {
          title: '记供应商账',
          dataIndex: 'isRecord',
          scopedSlots: { customRender: 'isRecord' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '220px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
    }
  },
  created() {
    this.handleProd()
  },
  methods: {
    removeTrim(array) {
      for (let i = 0; i < array.length; i++) {
        if (array[i] == '') {
          array.splice(i, 1)
          i = i - 1
        }
      }
      return array
    },
    handleDel(record) {
      const that = this
      that.$confirm({
        title: '删除',
        content: '你确定要删除这个商品吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          that.loadData.splice(
            that.loadData.findIndex((item) => item.id === record.id),
            1
          )
          // this.loadData = this.loadData
        },
      })
    },
    handleAdd(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loadData.push({
            ...values,
            phoneCode1: this.removeTrim(values.phoneCodeList.split('\n'))[0],
            phoneCode2: this.removeTrim(values.phoneCodeList.split('\n'))[1],
            goodName: this.prodOption.find((item) => {
              return item.id == values.product
            })?.name,
            goodId: this.prodOption.find((item) => {
              return item.id == values.product
            })?.id,
          })
        }
      })
    },

    handleProd() {
      getGoodsListUsingPOST(this.queryParam)
        .then((res) => {
          this.prodOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleSubmit() {
      this.formLoading = true
      const params = {
        // second: values.second,
        // memo: values.memo,
        // orderDate: values.orderDate,
        // supplyId: values.supplyId,
        orderId: this.$route.params.id,
        // channelId: values.channelId,
        // buyType: 1,
      }
      const data = {
        detailParamList: this.loadData.map((item) => {
          return {
            brandName: item.brandName,
            commCost: item.commCost,
            goodClass2: item.classId,
            goodsId: item.goodId,
            goodsName: item.goodName,
            isRecord: item.isRecord,
            phoneCode1: item.phoneCode1,
            phoneCode2: item.phoneCode2,
          }
        }),
      }

      appendPurchaseUsingPOST({ ...params, detailParamList: data.detailParamList })
        .then(() => {
          this.$message.success('操作成功')
          this.formLoading = false
          this.$router.back()
          this.form.resetFields()
          this.form1.resetFields()
        })
        .catch((error) => {
          this.formLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
  